package cn.smbms.controller;

import cn.smbms.pojo.User;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
public class IndexController{// extends AbstractController

    private Logger logger = Logger.getLogger(IndexController.class);

   /* protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception{
        System.out.println("hello,SpringMVC!");
        return new ModelAndView("index");
    }*/

   //示例6
  /* @RequestMapping("/index")
    public String index(){
       logger.info("hello,SpringMVC!");
       return "index";
   }*/

    /**
     * 参数传递View to Controller
     * @param username
     * @return
     */
    //@RequestMapping("/welcome")
    //示例9
    /*@RequestMapping(value = "/welcome",method=RequestMethod.GET,params="username")
    public String welcome(@RequestParam String username){
        logger.info("welcome,"+username);
        return "index";
    }*/

    //示例10
   /*@RequestMapping("/welcome")
    public String welcome(@RequestParam(value="username",required=false)String username){
        logger.info("welcome,"+username);
        return "index";
    }*/

   //示例11
  /* @RequestMapping("index1")
    public ModelAndView index(String username){
       logger.info("welcome! username"+username);
       ModelAndView mView = new ModelAndView();
       mView.addObject("username",username);
       mView.setViewName("index");
       return mView;
   }*/

   /* //示例13-14-15
    @RequestMapping("/index2")
    public String index(String username, Model model){
        logger.info("hello,SpringMVC! username:"+username);
        model.addAttribute("username",username);
        model.addAttribute(username);
        User user = new User();
        user.setUserName(username);
        model.addAttribute("currentUser",user);
        model.addAttribute(user);
        return "index";
    }*/

    //示例16
    @RequestMapping("/index3")
    public String index(String username,Map<String,Object> model){
        logger.info("hello,SpringMVC! username:"+username);
        model.put("username",username);
        return "index";
    }

}
